<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option v-for="item in options" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入创建者" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['system:notice:list']" type="primary" icon="el-icon-search" size="small"
          @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button v-hasPermi="['system:notice:add']" type="primary" plain icon="el-icon-plus" size="small"
          @click="handleAdd">新增</el-button>
        <el-button v-hasPermi="['system:notice:delete']" type="danger" plain icon="el-icon-delete" size="small"
          :disabled="multiple" @click="handleDelete">
          批量删除
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="noticeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="类型" align="center" prop="type">
      </el-table-column>
      <el-table-column label="内容" align="center" prop="content" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime">
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['system:notice:update']" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-hasPermi="['system:notice:delete']" size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination style="float: right;margin-top: 20px;" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" background :current-page="queryParams.currentPage"
      :page-sizes="[5,10,50,100]" :page-size="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择类型">
                <el-option v-for="item in options" :key="item" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <el-input v-model="form.content" type="textarea" :rows="2" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listNotice,
    getNotice,
    delNotice,
    addNotice,
    updateNotice
  } from "@/api/system/notice";

  export default {
    name: "Notice",
    data() {
      return {
        loading: true,
        options: ['通知', '公告'],
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 公告表格数据
        noticeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          currentPage: 1,
          pageSize: 10,
          title: undefined,
          createBy: undefined,
          type: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [{
            required: true,
            message: "公告标题不能为空",
            trigger: "blur"
          }],
          type: [{
            required: true,
            message: "公告类型不能为空",
            trigger: "change"
          }]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      handleCurrentChange(val) {
        this.queryParams.currentPage = val;
        this.getList();
      },
      handleSizeChange(val) {
        this.queryParams.pageSize = val;
        this.getList();
      },
      /** 查询公告列表 */
      getList() {
        this.loading = true
        listNotice(this.queryParams).then(response => {
          this.noticeList = response.data.records;
          this.total = response.data.total;
          this.loading = false
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          title: undefined,
          type: undefined,
          content: undefined
        };
        this.$refs['queryForm'].resetFields();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.currentPage = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs['queryForm'].resetFields();
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加公告";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        getNotice(row.id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改公告";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateNotice(this.form).then(response => {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addNotice(this.form).then(response => {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let ids = row.id || this.ids
        this.$confirm(`此操作删除id为【${ids}】的数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delNotice(ids).then(res => {
            this.getList();
            this.$message.success("删除成功");
          })
        }).catch(() => {});
      }
    }
  };
</script>
